﻿using System.Collections.Generic;
using TheCell;

namespace SudoCalc
{
    class CombinationUtils
    {
        public static void CombinationPossible(Dictionary<int, List<Cell>> Lines, Dictionary<int, List<Cell>> Columns, Dictionary<int, List<Cell>> Area)
        {
            CombinationDictionary = Combination(Lines, Columns, Area);
        }

        public static Dictionary<int, List<List<int>>> CombinationDictionary
        {
            get;
            private set;
        }

        private static Dictionary<int, List<List<int>>> Combination(Dictionary<int, List<Cell>> Lines, Dictionary<int, List<Cell>> Columns, Dictionary<int, List<Cell>> Areas)
        {
            Dictionary<int, List<int>> Linepossible = FiltrationUtils.Filtration(Lines);
            Dictionary<int, List<int>> Columpossible = FiltrationUtils.Filtration(Columns);
            Dictionary<int, List<int>> Areapossible = FiltrationUtils.Filtration(Areas);

            Dictionary<int, List<List<int>>> LineCombination = ZoneCombination(Linepossible);
            Dictionary<int, List<List<int>>> ColumCombination = ZoneCombination(Columpossible);
            Dictionary<int, List<List<int>>> AreaCombination = ZoneCombination(Areapossible);

            Dictionary<int, List<List<int>>> combination = new();

            combination = Marge(LineCombination, combination);
            combination = Marge(ColumCombination, combination);
            combination = Marge(AreaCombination, combination);

            return combination;
        }

        private static Dictionary<int, List<List<int>>> ZoneCombination(Dictionary<int, List<int>> zonepossible)
        {
            Dictionary<int, List<List<int>>> zonecombination = new();
            foreach (KeyValuePair<int, List<int>> possibles in zonepossible)
            {
                {
                    for (int i = 3; i <= possibles.Value.Count; i++)
                    {
                        List<List<int>> part = Combinationpossible(possibles.Value, i);
                        zonecombination.Add(i, part);
                    }
                }
            }
            return zonecombination;
        }

        private static List<List<int>> Combinationpossible(List<int>possibles,int i)
        {
            List<List<int>> list = new();
            for (int index = 1; index <= i; index++)//i为取多少个数
            {
                if (list.Count != 0)
                {
                    foreach (int id in possibles)
                    {
                        for (int j = 1; j <= possibles.Count; j++)//复制count遍，
                        {
                            if (!list[index].Contains(id))//但把已有的ID去掉。 
                            {

                            }
                        }
                    }
                }
                else
                {
                    list = Initialize(possibles, list); 
                }
            }
            return list;
        }

        /*TestModel[] copyArray = new TestModel[testList.Count];
        testList.CopyTo(copyArray);*/

        private static List<List<int>> Initialize(List<int> possibles, List<List<int>> list)//应该存指针
        {
            List<int> result = new();
            for (int i = 1; i <= possibles.Count; i++)
            {               
                    result.Add(possibles[i]);
                    list.Add(result);                
            }
            return list;
        }

        private static Dictionary<int, List<List<int>>> Marge(Dictionary<int, List<List<int>>> ZoneCombination, Dictionary<int, List<List<int>>> combination)
        {
            foreach (KeyValuePair<int, List<List<int>>> item in ZoneCombination)
            {
                combination.Add(item.Key, item.Value);
            }
            return combination;
        }
    }
}
